package nl.saxion.security.gui.elements;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import nl.saxion.security.config.Config;
import nl.saxion.security.init.MainModule;

import nl.saxion.security.serverconfig.ServerConfig;
import nl.saxion.security.synchronize.ServerSynchronize;

import com.adamtaft.eb.EventBusService;
import com.google.inject.Inject;
import com.google.inject.name.Named;

public class PathPanel extends AbstractPathPanel {

	private static final long serialVersionUID = 7004306013525921806L;
	
	private JLabel helpLabel;
	
	private static JTextField path;
	
	private JButton pathSelect;
	
	private File selected;
	
	private JButton startServerBtn;
	
	private Config config;
	
	private ServerConfig sConfig;
	
	@Inject
	public PathPanel(@Named("DefaultConfig") Config c)
	{
		super();
		config = c;
		configure();
		sConfig = ServerConfig.getSingletonObject(config);
	}
	
	protected void configure()
	{
		setLayout(new BorderLayout());
		configureHelpLabel();
		configurePathSelector();
		configureStartServerButton();
	}
	
	private void configureHelpLabel()
	{
		helpLabel = new JLabel("Selecteer een map als bron of doel voor de synchronisatie");
		add(helpLabel, BorderLayout.NORTH);
	}
	
	private void configurePathSelector()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		
		path = new JTextField();
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 4.0;
		c.weighty = 1.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.LINE_START;
		panel.add(path, c);
		
		pathSelect = new JButton("Map kiezen");
		pathSelect.addActionListener(new FileChooseAction());
		
		GridBagConstraints c2 = new GridBagConstraints();
		c2.gridx = 5;
		c2.gridy = 0;
		c2.weightx = 1.0;
		c2.weighty = 1.0;
		c2.fill = GridBagConstraints.HORIZONTAL;
		c2.anchor = GridBagConstraints.LINE_START;
		panel.add(pathSelect, c2);
		
		
		add(panel, BorderLayout.CENTER);
	}
	

	private class FileChooseAction implements ActionListener
	{

		final JFileChooser fc;
		
		public FileChooseAction()
		{
			fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			int style = PathPanel.this.getFileChooseStyle();
			int result = -1;
			
			if(style == AbstractPathPanel.STYLE_FILE_OPEN)
			{
				result = fc.showOpenDialog(PathPanel.this);
			} else if(style == AbstractPathPanel.STYLE_FILE_SAVE)
			{
				result = fc.showSaveDialog(PathPanel.this);
			}
			
			if(result == JFileChooser.APPROVE_OPTION)
			{
				selected = fc.getSelectedFile();
				PathPanel.this.path.setText(selected.toString());
				EventBusService.publish(createChoosenFileEvent(selected));
			}	

			config.setAsString(Config.KEY_SYNC_PATH, selected.getAbsolutePath());
			
			
			sConfig.changeDirectory();
		}
		
	}
	
	public static void setPathText(String Text) {
		path.setText(Text);
	}
	
	public String getPath(){
		return path.getText();
	}
	
	private void configureStartServerButton() {
		startServerBtn = new JButton();
		startServerBtn.setText("Start Synchroniseren");
		
		startServerBtn.addActionListener(new StartServerAction()); {
			
		}
		add(startServerBtn, BorderLayout.SOUTH);		
	}
	
	private class StartServerAction implements ActionListener {	
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub			
			sConfig.startserverSync();
			
		}
		
		
	}
	
	
}
